package com.ffcs.crmd.tsp.server.jdbc.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobDetailParam;
import com.ffcs.crmd.tsp.server.jdbc.repository.JobDetailParamRepository;
import com.ffcs.crmd.tsp.server.jdbc.service.IJobDetailParamService;

/**
 * 
 * 功能说明:任务服务
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class JobDetailParamServiceImpl implements IJobDetailParamService{
    
    private JobDetailParamRepository jobDetailParamRepository = new JobDetailParamRepository();
    
    
    public JobDetailParamServiceImpl() {
        
    }
    
    
    @Override
    public boolean add(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(TspSeqServiceImpl.getSeq());
        params.add(jobDetailParam.getMkey());
        params.add(jobDetailParam.getMvalue());
        params.add(jobDetailParam.getJobDetailId());
        params.add(jobDetailParam.getClusterName());
        return jobDetailParamRepository.executeJobDetailParams(INSERT_JOB_DETAIL_PARAM, params, conn);
    }
    
    @Override
    public void add(Connection conn, List<JobDetailParam> jobDetailParamList) throws TspException {
        for(JobDetailParam jobDetailParam : jobDetailParamList){
            add(conn, jobDetailParam);
        }
    }

    
    @Override
    public boolean delete(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpDelete(DELETE_JOB_DETAIL_PARAM, jobDetailParam, params);
        return jobDetailParamRepository.executeJobDetailParams(sql, params, conn);
    }
    
    
    @Override
    public JobDetailParam get(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobDetailParam.getId());
        params.add(jobDetailParam.getJobDetailId());
        params.add(jobDetailParam.getMkey());
        return jobDetailParamRepository.query(GET_JOB_DETAIL_PARAM, params, conn);
    }
    
    @Override
    public List<JobDetailParam> query(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_JOB_DETAIL_PARAM, jobDetailParam, params);
        return jobDetailParamRepository.queryList(sql, params, conn);
    }
    
    @Override
    public List<JobDetailParam> queryPage(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpPage(SELECT_JOB_DETAIL_PARAM, jobDetailParam, params);
        return jobDetailParamRepository.queryList(sql, params, conn);
    }
    
    @Override
    public int querySize(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobDetailParam.getJobDetailId());
        params.add(jobDetailParam.getClusterName());
        params.add("%" + jobDetailParam.getMkey() + "%");
        return jobDetailParamRepository.queryRows(GET_JOB_DETAIL_PARAM, params, conn);
    }
    
    
    @Override
    public boolean update(Connection conn, JobDetailParam jobDetailParam) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobDetailParam.getMvalue());
        params.add(jobDetailParam.getId());
        return jobDetailParamRepository.executeJobDetailParams(UPDATE_JOB_DETAIL_PARAM, params, conn);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobDetailParam
     * @param params
     * @return
     */
    private String rtp(String sql, JobDetailParam jobDetailParam, final List<Object> params){
        if(jobDetailParam.getJobDetailId() != null){
            sql += " AND " + COL_JOB_DETAIL_PARAM_JOB_DETAIL_ID + " = ?"; 
            params.add(jobDetailParam.getJobDetailId());
        }
        
        if(StringUtils.isNoneBlank(jobDetailParam.getMkey())){
            sql += " AND " + COL_JOB_DETAIL_PARAM_MEKY + " LIKE ?"; 
            params.add("%"+ jobDetailParam.getMkey() + "%");
        }
        
        if(StringUtils.isNoneBlank(jobDetailParam.getClusterName())){
            sql += " AND " + COL_CLUSTER_NAME + " = ?"; 
            params.add(jobDetailParam.getClusterName());
        }
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobDetail
     * @param params
     * @return
     * @throws TspException 
     */
    private String rtpDelete(String sql, JobDetailParam jobDetailParam, final List<Object> params) throws TspException {
        if (jobDetailParam.getJobDetailId() == null && jobDetailParam.getId() == null) {
            throw new TspException("['id' or 'jobDetailId'] can't be null at the same time！");
        }
        
        if (jobDetailParam.getId() != null) {
            sql += " AND " + COL_ID + " = ?";
            params.add(jobDetailParam.getId());
        }
        
        if (jobDetailParam.getJobDetailId() != null) {
            sql += " AND " + COL_JOB_DETAIL_PARAM_JOB_DETAIL_ID + " = ?";
            params.add(jobDetailParam.getJobDetailId());
        }
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobDetailParam
     * @param params
     * @return
     */
    private String rtpPage(String sql, JobDetailParam jobDetailParam, final List<Object> params){
        String rsql = rtp(sql, jobDetailParam, params);
        rsql += " LIMIT ?,?";
        params.add(jobDetailParam.getPageNumber());
        params.add(jobDetailParam.getPageSize());
        return rsql;
    }

    
}
